package com.ruoyi.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.domain.BizLight;
import com.ruoyi.business.domain.BizPlan;
import com.ruoyi.business.domain.BizScore;
import com.ruoyi.business.domain.BizUser;
import com.ruoyi.business.service.BizLightDataService;
import com.ruoyi.business.service.BizPlanDataService;
import com.ruoyi.business.service.BizScoreDataService;
import com.ruoyi.business.service.BizUserDataService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/business/gzh")
public class BizGzhController extends BaseController {



    @Autowired
    private BizUserDataService bizUserDataService;

    @Autowired
    private BizPlanDataService bizPlanDataService;

    @Autowired
    private BizScoreDataService bizScoreDataService;

    @Autowired
    private BizLightDataService bizLightDataService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @GetMapping("/user/list")
    public TableDataInfo userList() {
        Page<BizUser> page = getPage();
        String userNumber = ServletUtils.getParameter("userNumber");
        QueryWrapper<BizUser> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(userNumber)){
            queryWrapper.eq("user_number",userNumber);
        }
        page = bizUserDataService.page(page,queryWrapper);
        return getDataTable(page);
    }


    @Log(title = "教务信息导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @PostMapping("/user/importData")
    public AjaxResult userImport(MultipartFile file) throws Exception {

        ExcelUtil<BizUser> util = new ExcelUtil<BizUser>(BizUser.class);
        List<BizUser> userList = util.importExcel(file.getInputStream());

        bizUserDataService.delete(new QueryWrapper<>());
        userList.forEach(user ->
                {
                    bizUserDataService.save(user);
                }
        );


        return success();
    }

    @Log(title = "教务信息导出", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @PostMapping("/user/exportData")
    public void userExport(HttpServletResponse response) throws Exception {

        ExcelUtil<BizUser> util = new ExcelUtil<>(BizUser.class);

        List<BizUser> bizUsers = bizUserDataService.selectAll();

        util.exportExcel(response, bizUsers, "教务信息");
    }

    @Log(title = "修改评语和荣誉", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @PostMapping("/set/comment")
    public AjaxResult setComment(@RequestBody BizUser bizUser) throws Exception {

        BizUser user = bizUserDataService.getById(bizUser.getId());

        if(Objects.nonNull(user)){
            user.setComment(bizUser.getComment());
            user.setHonour(bizUser.getHonour());

            bizUserDataService.save(user);
        }

        return success();
    }

    @Log(title = "添加教务信息", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @PostMapping("/user/add")
    public AjaxResult addUser(@RequestBody BizUser bizUser) throws Exception {

        bizUserDataService.save(bizUser);

        return success();
    }


    @PreAuthorize("@ss.hasPermi('business:plan:list')")
    @GetMapping("/plan/list")
    public TableDataInfo planList() {
        Page<BizPlan> page = getPage();
        page = bizPlanDataService.page(page);
        return getDataTable(page);
    }


    @Log(title = "教学计划导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:plan:list')")
    @PostMapping("/plan/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {

        ExcelUtil<BizPlan> util = new ExcelUtil<BizPlan>(BizPlan.class);
        List<BizPlan> userList = util.importExcel(file.getInputStream());
        bizPlanDataService.delete(new QueryWrapper<>());
        userList.forEach(user ->
                {
                    bizPlanDataService.save(user);
                }
        );


        return success();
    }


    @Log(title = "教学计划导出", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @PostMapping("/plan/exportData")
    public void planExport(HttpServletResponse response) throws Exception {

        ExcelUtil<BizPlan> util = new ExcelUtil<>(BizPlan.class);

        List<BizPlan> bizUsers = bizPlanDataService.selectAll();

        util.exportExcel(response, bizUsers, "教学计划");
    }


    @PreAuthorize("@ss.hasPermi('business:score:list')")
    @GetMapping("/score/list")
    public TableDataInfo scoreList() {
        Page<BizScore> page = getPage();
        String userNumber = ServletUtils.getParameter("userNumber");
        QueryWrapper<BizScore> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(userNumber)){
            queryWrapper.eq("user_number",userNumber);
        }
        page = bizScoreDataService.page(page,queryWrapper);
        return getDataTable(page);
    }


    @Log(title = "考勤得分导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:score:list')")
    @PostMapping("/score/importData")
    public AjaxResult scoreImport(MultipartFile file) throws Exception {

        ExcelUtil<BizScore> util = new ExcelUtil<BizScore>(BizScore.class);
        List<BizScore> userList = util.importExcel(file.getInputStream());

        //bizScoreDataService.delete(new QueryWrapper<>());
        userList.forEach(user ->
                {
                    bizScoreDataService.save(user);
                }
        );


        return success();
    }

    @Log(title = "考勤得分删除", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('business:score:list')")
    @GetMapping("/score/delete")
    public AjaxResult scoreDelete(@RequestParam("id") int id) throws Exception {

        bizScoreDataService.removeById(id);

        return success();
    }

    @Log(title = "考勤得分导出", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @PostMapping("/score/exportData")
    public void scoreExport(HttpServletResponse response) throws Exception {

        ExcelUtil<BizScore> util = new ExcelUtil<>(BizScore.class);

        List<BizScore> bizUsers = bizScoreDataService.selectAll();

        util.exportExcel(response, bizUsers, "教学计划");
    }

    @PreAuthorize("@ss.hasPermi('business:light:list')")
    @GetMapping("/light/list")
    public TableDataInfo lightList() {
        Page<BizLight> page = getPage();
        String fromNumber = ServletUtils.getParameter("fromNumber");
        String toNumber = ServletUtils.getParameter("toNumber");
        QueryWrapper<BizLight> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(fromNumber)){
            queryWrapper.eq("from_number",fromNumber);
        }
        if(StringUtils.isNotEmpty(toNumber)){
            queryWrapper.eq("to_number",toNumber);
        }
        page = bizLightDataService.page(page,queryWrapper);
        return getDataTable(page);
    }


    @Log(title = "灯脉数据导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:light:list')")
    @PostMapping("/light/importData")
    public AjaxResult lightImport(MultipartFile file) throws Exception {

        ExcelUtil<BizLight> util = new ExcelUtil<BizLight>(BizLight.class);
        List<BizLight> userList = util.importExcel(file.getInputStream());

        bizLightDataService.delete(new QueryWrapper<>());
        userList.forEach(user ->
                {
                    bizLightDataService.save(user);
                }
        );


        return success();
    }


    @Log(title = "下载模版", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:light:list')")
    @PostMapping("/light/exportData")
    public void exportTemplate(HttpServletResponse response) {

        ExcelUtil<BizLight> util = new ExcelUtil<>(BizLight.class);
        List<BizLight> data = bizLightDataService.selectAll();

        util.exportExcel(response, data, "灯脉信息");
    }
}
